package com.demo.controller;

import com.demo.exception.CustomerException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("order")
public class OrderController2 {

    @RequestMapping("getOrderDetail1")
    public String getOrderById(@RequestParam("id") Long id) {
        if (id < 0) {
            throw new CustomerException("传入ID是不能小于0的");
        }

        int i = 2 / 0;

        return "orderDetail";
    }

    @ExceptionHandler // 侵入式异常处理方式
    public String hanlderExecption(Exception e, Model model) {
        System.out.println("--inner--");
        if (e instanceof CustomerException) {
            model.addAttribute("ex", e);
            return "cus_error";
        } else if (e instanceof Exception) {
            model.addAttribute("ex", e);
            return "sys_error";
        } else {
            model.addAttribute("ex", e);
            return "global_error";
        }
    }
}
